﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DbHelp;
using DevExpress.XtraEditors;
using DevExpress.Utils;

namespace IotherRis
{
    public partial class Uc_BaseInfo : UserControl
    {
        public Uc_BaseInfo()
        {
            InitializeComponent();
        }
        uDataSetC mUds;
        DataTable mDtReturn;
        bool mIsTrue;

        Fr_main_other mMain_other;

        public Fr_main_other MMain_other
        {
            get { return mMain_other; }
            set { mMain_other = value; }
        }

        public bool MIsTrue
        {
            get { return mIsTrue; }
            set { mIsTrue = value; }
        }
        public DataTable SDtReturn
        {
            get { return mDtReturn; }
            set { mDtReturn = value; }
        }

        #region 事件

        private void gridControl_dengji_DoubleClick(object sender, EventArgs e)
        {
            //详情
            try
            {
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hi = gridView2.CalcHitInfo((sender as Control).PointToClient(Control.MousePosition));

                if (hi.RowHandle >= 0)
                {
                    string strPatid = gridView2.GetRowCellValue(hi.RowHandle, "IID").ToString();
                    Fr_BaseinfoDetail mReport_se_detail = new Fr_BaseinfoDetail(this, strPatid);
                    mReport_se_detail.ShowDialog();

                    if (MIsTrue)
                    {
                        filterRunparam(xTabC_dengji_sz.SelectedTabPage.Name);
                    }
                }
            }
            catch { }
        }

        private void tbi_query_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (xTabC_dengji_sz.SelectedTabPage != null)
            {
                filterRunparam(xTabC_dengji_sz.SelectedTabPage.Name);
            }
        }

        private void tbi_adds_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //新增
            Fr_BaseinfoDetail mReport_detail = new Fr_BaseinfoDetail(this, "-1");
            mReport_detail.ShowDialog();

            if (MIsTrue)
            {
                if (xTabC_dengji_sz.SelectedTabPage != null)
                {
                    filterRunparam(xTabC_dengji_sz.SelectedTabPage.Name);
                }
            }

        }

        private void tbi_detail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //详情
            try
            {
                int mHand = gridView2.FocusedRowHandle;
                if (mHand >= 0)
                {

                    string strPatid = gridView2.GetRowCellValue(mHand, "IID").ToString();
                    Fr_BaseinfoDetail mReport_se_detail = new Fr_BaseinfoDetail(this, strPatid);
                    mReport_se_detail.ShowDialog();
                    if (MIsTrue)
                    {
                        filterRunparam(xTabC_dengji_sz.SelectedTabPage.Name);
                    }
                }
            }
            catch
            {
            }
        }

        private void tbi_exit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //this.Close();
        }

        private void tbi_delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int mHand = gridView2.FocusedRowHandle;
            if (mHand >= 0)
            {
                //删除
                if (XtraMessageBox.Show("确认要删除吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }


                uDataSetC mDsDelete;
                WaitDialogForm wForm = new WaitDialogForm("正在打开检查，请稍候 ... ...", "提示");
                try
                {

                    // DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hi = gridView2.CalcHitInfo(gridControl_dengji.PointToClient(Control.MousePosition));


                    string strPatid = gridView2.GetRowCellValue(mHand, "IID").ToString();
                    string mSql = "select * from PI_BASEINFO where IID =" + strPatid;

                    //使用构造函数，一起初始化传递的参数
                    mDsDelete = new uDataSetC(mSql, AccessHelper.mReportDb);
                    mDsDelete.DeleteRow(1);
                    mDsDelete.Update(AccessHelper.mReportDb);
                    gridView2.DeleteRow(mHand);
                    wForm.Close();
                }
                catch
                {
                    XtraMessageBox.Show("删除失败");
                    wForm.Close();
                }
                wForm.Close();
            }
            else
            {
                XtraMessageBox.Show("请先选择需要删除数据");
            }


        }

        private void tbi_print_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView2.OptionsPrint.UsePrintStyles = true;
            gridControl_dengji.LookAndFeel.SkinName = "";
            // PrintView(gridControl_dengji, "登记报告属性列表");
        }


        private void xTabC_dengji_sz_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            if (e.Page != null)
            {
                filterRunparam(e.Page.Name);
            }
        }

        private void bti_type_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xTabC_dengji_sz.TabPages.Clear();
            addTabPages();
        }
        #endregion

        #region 方法

        //添加xTabC_report_sz的tabpages
        private void addTabPages()
        {
            //ris系统report param的paramtype项没有下划线
            string mSql = "select * from PI_BASEINFO where ipid=0 order by iordernum ";
            uDataSetC mUds_tabpage = new uDataSetC(mSql, AccessHelper.mReportDb);

            if (mUds_tabpage.RowCount() > 0)
            {
                for (int i = 1; i <= mUds_tabpage.RowCount(); i++)
                {
                    DevExpress.XtraTab.XtraTabPage mTabPage = new DevExpress.XtraTab.XtraTabPage();
                    mTabPage.Name = mUds_tabpage.GetItemString(i, "iid").ToString();
                    mTabPage.Text = mUds_tabpage.GetItemString(i, "cparamdesc").ToString();
                    mTabPage.Size = new System.Drawing.Size(0, 0);
                    mTabPage.Height = 30;

                    xTabC_dengji_sz.TabPages.Add(mTabPage);
                }
                xTabC_dengji_sz.SelectedTabPageIndex = 0;
                xTabC_dengji_sz.SelectedPageChanged += new DevExpress.XtraTab.TabPageChangedEventHandler(this.xTabC_dengji_sz_SelectedPageChanged);
            }
            else
            {
                return;
            }
        }

        /// <summary>
        /// 按选中的tabpage，显示相应的runparnam
        /// </summary>
        /// <param name="pTabName">选中的tabpage name</param>
        private void filterRunparam(string pTabID)
        {

            string mSql = @"select * from PI_BASEINFO where ipid=" + pTabID + " order by iordernum";

            uDataSetC mUds_filter = new uDataSetC(mSql, AccessHelper.mReportDb);
            if (mUds_filter.RowCount() > 0)
            {
                gridControl_dengji.DataSource = mUds_filter.Tables[0];

            }
            else
            {
                gridControl_dengji.DataSource = null;

            }
        }

        #endregion

        private void bti_close_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //this.Close();
        }

        private void lbi_add_type_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Fr_baseType mReport_type = new Fr_baseType();
            mReport_type.ShowDialog();
            xTabC_dengji_sz.TabPages.Clear();
            addTabPages();
        }

        private void Uc_BaseInfo_Load(object sender, EventArgs e)
        {
            addTabPages();
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            if (xTabC_dengji_sz.SelectedTabPage != null)
            {
                filterRunparam(xTabC_dengji_sz.SelectedTabPage.Name);
            }
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            //新增
            Fr_BaseinfoDetail mReport_detail = new Fr_BaseinfoDetail(this, "-1");
            mReport_detail.ShowDialog();


            if (xTabC_dengji_sz.SelectedTabPage != null)
            {
                filterRunparam(xTabC_dengji_sz.SelectedTabPage.Name);
            }

            MMain_other.f_loadBaseInfo();
            //刷新数据
        }

        private void btndetail_Click(object sender, EventArgs e)
        {
            //详情
            try
            {
                int mHand = gridView2.FocusedRowHandle;
                if (mHand >= 0)
                {

                    string strPatid = gridView2.GetRowCellValue(mHand, "IID").ToString();
                    Fr_BaseinfoDetail mReport_se_detail = new Fr_BaseinfoDetail(this, strPatid);
                    mReport_se_detail.ShowDialog();

                    filterRunparam(xTabC_dengji_sz.SelectedTabPage.Name);
                    MMain_other.f_loadBaseInfo();
                }
            }
            catch
            {
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            int mHand = gridView2.FocusedRowHandle;
            if (mHand >= 0)
            {
                //删除
                if (XtraMessageBox.Show("确认要删除吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }


                uDataSetC mDsDelete;
                WaitDialogForm wForm = new WaitDialogForm("正在打开检查，请稍候 ... ...", "提示");
                try
                {

                    // DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hi = gridView2.CalcHitInfo(gridControl_dengji.PointToClient(Control.MousePosition));


                    string strPatid = gridView2.GetRowCellValue(mHand, "IID").ToString();
                    string mSql = "select * from PI_BASEINFO where IID =" + strPatid;

                    //使用构造函数，一起初始化传递的参数
                    mDsDelete = new uDataSetC(mSql, AccessHelper.mReportDb);
                    mDsDelete.DeleteRow(1);
                    mDsDelete.Update(AccessHelper.mReportDb);
                    gridView2.DeleteRow(mHand);
                    wForm.Close();
                    MMain_other.f_loadBaseInfo();
                }
                catch
                {
                    XtraMessageBox.Show("删除失败");
                    wForm.Close();
                }
                wForm.Close();
            }
            else
            {
                XtraMessageBox.Show("请先选择需要删除数据");
            }


        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            xTabC_dengji_sz.TabPages.Clear();
            addTabPages();
        }
    }
}